package com.qianli.user.facade.query.impl.auth;

import com.fqgj.common.api.Response;
import com.qianli.user.UserHeadRequest;
import com.qianli.user.application.UserAuthApplication;
import com.qianli.user.facade.query.auth.UserAuthQueryServiceFacade;
import com.qianli.user.ro.UserStateCode;
import com.qianli.user.ro.auth.CustomerRO;
import com.qianli.user.ro.auth.UserAuthInfoRO;
import com.qianli.user.ro.query.UserAuthQueryRO;
import com.qianli.user.ro.query.UserCustomerQueryRO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * name:认证查询
 * auth:ting.li@qianli-inc.com
 * date:2018/1/9 09:42
 * version: v1.0.0
 **/
@Service("userAuthQueryServiceFacade")
public class UserAuthQueryServiceFacadeImpl implements UserAuthQueryServiceFacade {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserAuthQueryServiceFacade.class);

    @Autowired
    private UserAuthApplication userAuthApplication;

    /**
     * 获取实名信息
     *
     * @param query userCode
     * @return
     */
    @Override
    public Response<CustomerRO> getCustomer(UserCustomerQueryRO query) {
        String appCode = UserHeadRequest.getThreadLocal().getAppCode();

        if (null == query) {
            LOGGER.info("UserAuthQueryServiceFacade.getCustomer param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if (null == query.getAppCode() || null == query.getBiz()) {
            LOGGER.info("UserAuthQueryServiceFacade.getCustomer param appCode and biz can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "appCode and biz 不能为空");
        }
        if (null == query.getUserCode()) {
            LOGGER.info("UserAuthQueryServiceFacade.getCustomer param userCode  can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userCode 不能都为空");
        }
        Response<UserAuthInfoRO> response = userAuthApplication.load(query.getUserCode());
        if (!response.isSuccess()) {
            return Response.error(UserStateCode.getEnumByType(response.getCode()));
        }
        UserAuthInfoRO userAuthInfoRO = response.getData();
        CustomerRO customerRO = userAuthInfoRO.getCustomer();
        return Response.ok(customerRO);
    }

    /**
     * 获取认证信息
     *
     * @param query
     * @return
     */
    @Override
    public Response<UserAuthInfoRO> getUserCreditAuth(UserAuthQueryRO query) {
        if (null == query) {
            LOGGER.info("UserAuthQueryServiceFacade.getUserCreditAuth param can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK);
        }
        if (null == query.getAppCode() || null == query.getBiz()) {
            LOGGER.info("UserAuthQueryServiceFacade.getUserCreditAuth param appCode and biz can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "appCode and biz 不能为空");
        }
        if (null == query.getUserCode()) {
            LOGGER.info("UserAuthQueryServiceFacade.getUserCreditAuth param userCode can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "userCode不能为空");
        }
        if (null == query.getAuthType()) {
            LOGGER.info("UserAuthQueryServiceFacade.getUserCreditAuth param authType can not be null");
            return Response.error(UserStateCode.PARAMETER_LACK, "authType不能为空");
        }

        Response<UserAuthInfoRO> response = userAuthApplication.load(query.getUserCode(), query.getAuthType());
        if (!response.isSuccess()) {
            return Response.error(UserStateCode.getEnumByType(response.getCode()));
        }

        return response;
    }
}
